<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>textrange_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_dom_textrange_test.js.html">textrange_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.dom.TextRangeTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.dom.TextRangeTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom.ControlRange&#39;);
<a name="line20"></a>goog.require(&#39;goog.dom.Range&#39;);
<a name="line21"></a>goog.require(&#39;goog.dom.TextRange&#39;);
<a name="line22"></a>goog.require(&#39;goog.math.Coordinate&#39;);
<a name="line23"></a>goog.require(&#39;goog.style&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.ExpectedFailures&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line26"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line27"></a>
<a name="line28"></a>var logo;
<a name="line29"></a>var logo2;
<a name="line30"></a>var logo3;
<a name="line31"></a>var logo3Rtl;
<a name="line32"></a>var table;
<a name="line33"></a>var table2;
<a name="line34"></a>var table2div;
<a name="line35"></a>var test3;
<a name="line36"></a>var test3Rtl;
<a name="line37"></a>var expectedFailures;
<a name="line38"></a>
<a name="line39"></a>function setUpPage() {
<a name="line40"></a>  logo = goog.dom.getElement(&#39;logo&#39;);
<a name="line41"></a>  logo2 = goog.dom.getElement(&#39;logo2&#39;);
<a name="line42"></a>  logo3 = goog.dom.getElement(&#39;logo3&#39;);
<a name="line43"></a>  logo3Rtl = goog.dom.getElement(&#39;logo3Rtl&#39;);
<a name="line44"></a>  table = goog.dom.getElement(&#39;table&#39;);
<a name="line45"></a>  table2 = goog.dom.getElement(&#39;table2&#39;);
<a name="line46"></a>  table2div = goog.dom.getElement(&#39;table2div&#39;);
<a name="line47"></a>  test3 = goog.dom.getElement(&#39;test3&#39;);
<a name="line48"></a>  test3Rtl = goog.dom.getElement(&#39;test3Rtl&#39;);
<a name="line49"></a>  expectedFailures = new goog.testing.ExpectedFailures();
<a name="line50"></a>}
<a name="line51"></a>
<a name="line52"></a>function tearDown() {
<a name="line53"></a>  expectedFailures.handleTearDown();
<a name="line54"></a>}
<a name="line55"></a>
<a name="line56"></a>function testCreateFromNodeContents() {
<a name="line57"></a>  assertNotNull(&#39;Text range object can be created for element node&#39;,
<a name="line58"></a>      goog.dom.TextRange.createFromNodeContents(logo));
<a name="line59"></a>  assertNotNull(&#39;Text range object can be created for text node&#39;,
<a name="line60"></a>      goog.dom.TextRange.createFromNodeContents(logo2.previousSibling));
<a name="line61"></a>}
<a name="line62"></a>
<a name="line63"></a>function testMoveToNodes() {
<a name="line64"></a>  var range = goog.dom.TextRange.createFromNodeContents(table2);
<a name="line65"></a>  range.moveToNodes(table2div, 0, table2div, 1, false);
<a name="line66"></a>  assertEquals(&#39;Range should start in table2div&#39;,
<a name="line67"></a>               table2div,
<a name="line68"></a>               range.getStartNode());
<a name="line69"></a>  assertEquals(&#39;Range should end in table2div&#39;,
<a name="line70"></a>               table2div,
<a name="line71"></a>               range.getEndNode());
<a name="line72"></a>  assertEquals(&#39;Range start offset should be 0&#39;,
<a name="line73"></a>               0,
<a name="line74"></a>               range.getStartOffset());
<a name="line75"></a>  assertEquals(&#39;Range end offset should be 0&#39;,
<a name="line76"></a>               1,
<a name="line77"></a>               range.getEndOffset());
<a name="line78"></a>  assertFalse(&#39;Range should not be reversed&#39;,
<a name="line79"></a>              range.isReversed());
<a name="line80"></a>  range.moveToNodes(table2div, 0, table2div, 1, true);
<a name="line81"></a>  assertTrue(&#39;Range should be reversed&#39;,
<a name="line82"></a>             range.isReversed());
<a name="line83"></a>  assertEquals(&#39;Range text should be &quot;foo&quot;&#39;,
<a name="line84"></a>               &#39;foo&#39;,
<a name="line85"></a>               range.getText());
<a name="line86"></a>}
<a name="line87"></a>
<a name="line88"></a>function testContainsTextRange() {
<a name="line89"></a>  var range = goog.dom.TextRange.createFromNodeContents(table2);
<a name="line90"></a>  var range2 = goog.dom.TextRange.createFromNodeContents(table2div);
<a name="line91"></a>  assertTrue(&#39;TextRange contains other TextRange&#39;,
<a name="line92"></a>      range.containsRange(range2));
<a name="line93"></a>  assertFalse(&#39;TextRange does not contain other TextRange&#39;,
<a name="line94"></a>      range2.containsRange(range));
<a name="line95"></a>
<a name="line96"></a>  range = goog.dom.Range.createFromNodes(
<a name="line97"></a>      table2div.firstChild, 1, table2div.lastChild, 1);
<a name="line98"></a>  range2 = goog.dom.TextRange.createFromNodes(
<a name="line99"></a>      table2div.firstChild, 0, table2div.lastChild, 0);
<a name="line100"></a>  assertTrue(&#39;TextRange partially contains other TextRange&#39;,
<a name="line101"></a>      range2.containsRange(range, true));
<a name="line102"></a>  assertFalse(&#39;TextRange does not fully contain other TextRange&#39;,
<a name="line103"></a>      range2.containsRange(range, false));
<a name="line104"></a>}
<a name="line105"></a>
<a name="line106"></a>function testContainsControlRange() {
<a name="line107"></a>  if (goog.userAgent.IE) {
<a name="line108"></a>    var range = goog.dom.ControlRange.createFromElements(table2);
<a name="line109"></a>    var range2 = goog.dom.TextRange.createFromNodeContents(table2div);
<a name="line110"></a>    assertFalse(&#39;TextRange does not contain ControlRange&#39;,
<a name="line111"></a>        range2.containsRange(range));
<a name="line112"></a>    range = goog.dom.ControlRange.createFromElements(logo2);
<a name="line113"></a>    assertTrue(&#39;TextRange contains ControlRange&#39;,
<a name="line114"></a>        range2.containsRange(range));
<a name="line115"></a>    range = goog.dom.TextRange.createFromNodeContents(table2);
<a name="line116"></a>    range2 = goog.dom.ControlRange.createFromElements(logo, logo2);
<a name="line117"></a>    assertTrue(&#39;TextRange partially contains ControlRange&#39;,
<a name="line118"></a>        range2.containsRange(range, true));
<a name="line119"></a>    assertFalse(&#39;TextRange does not fully contain ControlRange&#39;,
<a name="line120"></a>        range2.containsRange(range, false));
<a name="line121"></a>  }
<a name="line122"></a>}
<a name="line123"></a>
<a name="line124"></a>function testGetStartPosition() {
<a name="line125"></a>  expectedFailures.expectFailureFor(goog.userAgent.GECKO &amp;&amp;
<a name="line126"></a>      !goog.userAgent.isVersionOrHigher(&#39;2&#39;));
<a name="line127"></a>
<a name="line128"></a>  // The start node is in the top left.
<a name="line129"></a>  var range = goog.dom.TextRange.createFromNodeContents(test3);
<a name="line130"></a>  var topLeft = goog.style.getPageOffset(test3.firstChild);
<a name="line131"></a>
<a name="line132"></a>  if (goog.userAgent.IE) {
<a name="line133"></a>    // On IE the selection is as tall as its tallest element.
<a name="line134"></a>    var logoPosition = goog.style.getPageOffset(logo3);
<a name="line135"></a>    topLeft.y = logoPosition.y;
<a name="line136"></a>
<a name="line137"></a>    if (!goog.userAgent.isVersionOrHigher(&#39;8&#39;)) {
<a name="line138"></a>      topLeft.x += 2;
<a name="line139"></a>      topLeft.y += 2;
<a name="line140"></a>    }
<a name="line141"></a>  }
<a name="line142"></a>
<a name="line143"></a>  try {
<a name="line144"></a>    var result = assertNotThrows(goog.bind(range.getStartPosition, range));
<a name="line145"></a>    assertObjectEquals(topLeft, result);
<a name="line146"></a>  } catch (e) {
<a name="line147"></a>    expectedFailures.handleException(e);
<a name="line148"></a>  }
<a name="line149"></a>}
<a name="line150"></a>
<a name="line151"></a>function testGetStartPositionNotInDocument() {
<a name="line152"></a>  expectedFailures.expectFailureFor(goog.userAgent.GECKO &amp;&amp;
<a name="line153"></a>      !goog.userAgent.isVersionOrHigher(&#39;2&#39;));
<a name="line154"></a>  expectedFailures.expectFailureFor(goog.userAgent.IE &amp;&amp;
<a name="line155"></a>      !goog.userAgent.isVersionOrHigher(&#39;8&#39;));
<a name="line156"></a>
<a name="line157"></a>  var range = goog.dom.TextRange.createFromNodeContents(test3);
<a name="line158"></a>
<a name="line159"></a>  goog.dom.removeNode(test3);
<a name="line160"></a>  try {
<a name="line161"></a>    var result = assertNotThrows(goog.bind(range.getStartPosition, range));
<a name="line162"></a>    assertNull(result);
<a name="line163"></a>  } catch (e) {
<a name="line164"></a>    expectedFailures.handleException(e);
<a name="line165"></a>  } finally {
<a name="line166"></a>    goog.dom.appendChild(document.body, test3);
<a name="line167"></a>  }
<a name="line168"></a>}
<a name="line169"></a>
<a name="line170"></a>function testGetStartPositionReversed() {
<a name="line171"></a>  expectedFailures.expectFailureFor(goog.userAgent.GECKO &amp;&amp;
<a name="line172"></a>      !goog.userAgent.isVersionOrHigher(&#39;2&#39;));
<a name="line173"></a>
<a name="line174"></a>  // Simulate the user selecting backwards from right-to-left.
<a name="line175"></a>  // The start node is now in the bottom right.
<a name="line176"></a>  var firstNode = test3.firstChild.firstChild;
<a name="line177"></a>  var lastNode = test3.lastChild.lastChild;
<a name="line178"></a>  var range = goog.dom.TextRange.createFromNodes(
<a name="line179"></a>      lastNode, lastNode.nodeValue.length, firstNode, 0);
<a name="line180"></a>  var pageOffset = goog.style.getPageOffset(test3.lastChild);
<a name="line181"></a>  var bottomRight = new goog.math.Coordinate(
<a name="line182"></a>      pageOffset.x + test3.lastChild.offsetWidth,
<a name="line183"></a>      pageOffset.y + test3.lastChild.offsetHeight);
<a name="line184"></a>
<a name="line185"></a>  if (goog.userAgent.IE &amp;&amp; !goog.userAgent.isVersionOrHigher(&#39;8&#39;)) {
<a name="line186"></a>    bottomRight.x += 2;
<a name="line187"></a>    bottomRight.y += 2;
<a name="line188"></a>  }
<a name="line189"></a>
<a name="line190"></a>  try {
<a name="line191"></a>    var result = assertNotThrows(goog.bind(range.getStartPosition, range));
<a name="line192"></a>    assertObjectRoughlyEquals(bottomRight, result, 1);
<a name="line193"></a>  } catch (e) {
<a name="line194"></a>    expectedFailures.handleException(e);
<a name="line195"></a>  }
<a name="line196"></a>}
<a name="line197"></a>
<a name="line198"></a>function testGetStartPositionRightToLeft() {
<a name="line199"></a>  expectedFailures.expectFailureFor(goog.userAgent.GECKO &amp;&amp;
<a name="line200"></a>      !goog.userAgent.isVersionOrHigher(&#39;2&#39;));
<a name="line201"></a>
<a name="line202"></a>  // Even in RTL content the start node is still in the top left.
<a name="line203"></a>  var range = goog.dom.TextRange.createFromNodeContents(test3Rtl);
<a name="line204"></a>  var topLeft = goog.style.getPageOffset(test3Rtl.firstChild);
<a name="line205"></a>
<a name="line206"></a>  if (goog.userAgent.IE) {
<a name="line207"></a>    // On IE the selection is as tall as its tallest element.
<a name="line208"></a>    var logoPosition = goog.style.getPageOffset(logo3Rtl);
<a name="line209"></a>    topLeft.y = logoPosition.y;
<a name="line210"></a>
<a name="line211"></a>    if (!goog.userAgent.isVersionOrHigher(&#39;8&#39;)) {
<a name="line212"></a>      topLeft.x += 2;
<a name="line213"></a>      topLeft.y += 2;
<a name="line214"></a>    }
<a name="line215"></a>  }
<a name="line216"></a>
<a name="line217"></a>  try {
<a name="line218"></a>    var result = assertNotThrows(goog.bind(range.getStartPosition, range));
<a name="line219"></a>    assertObjectEquals(topLeft, result);
<a name="line220"></a>  } catch (e) {
<a name="line221"></a>    expectedFailures.handleException(e);
<a name="line222"></a>  }
<a name="line223"></a>}
<a name="line224"></a>
<a name="line225"></a>function testGetEndPosition() {
<a name="line226"></a>  expectedFailures.expectFailureFor(goog.userAgent.GECKO &amp;&amp;
<a name="line227"></a>      !goog.userAgent.isVersionOrHigher(&#39;2&#39;));
<a name="line228"></a>
<a name="line229"></a>  // The end node is in the bottom right.
<a name="line230"></a>  var range = goog.dom.TextRange.createFromNodeContents(test3);
<a name="line231"></a>  var pageOffset = goog.style.getPageOffset(test3.lastChild);
<a name="line232"></a>  var bottomRight = new goog.math.Coordinate(
<a name="line233"></a>      pageOffset.x + test3.lastChild.offsetWidth,
<a name="line234"></a>      pageOffset.y + test3.lastChild.offsetHeight);
<a name="line235"></a>
<a name="line236"></a>  if (goog.userAgent.IE &amp;&amp; !goog.userAgent.isVersionOrHigher(&#39;8&#39;)) {
<a name="line237"></a>    bottomRight.x += 6;
<a name="line238"></a>    bottomRight.y += 2;
<a name="line239"></a>  }
<a name="line240"></a>
<a name="line241"></a>  try {
<a name="line242"></a>    var result = assertNotThrows(goog.bind(range.getEndPosition, range));
<a name="line243"></a>    assertObjectRoughlyEquals(bottomRight, result, 1);
<a name="line244"></a>  } catch (e) {
<a name="line245"></a>    expectedFailures.handleException(e);
<a name="line246"></a>  }
<a name="line247"></a>}
<a name="line248"></a>
<a name="line249"></a>function testGetEndPositionNotInDocument() {
<a name="line250"></a>  expectedFailures.expectFailureFor(goog.userAgent.GECKO &amp;&amp;
<a name="line251"></a>      !goog.userAgent.isVersionOrHigher(&#39;2&#39;));
<a name="line252"></a>  expectedFailures.expectFailureFor(goog.userAgent.IE &amp;&amp;
<a name="line253"></a>      !goog.userAgent.isVersionOrHigher(&#39;8&#39;));
<a name="line254"></a>
<a name="line255"></a>  var range = goog.dom.TextRange.createFromNodeContents(test3);
<a name="line256"></a>
<a name="line257"></a>  goog.dom.removeNode(test3);
<a name="line258"></a>  try {
<a name="line259"></a>    var result = assertNotThrows(goog.bind(range.getEndPosition, range));
<a name="line260"></a>    assertNull(result);
<a name="line261"></a>  } catch (e) {
<a name="line262"></a>    expectedFailures.handleException(e);
<a name="line263"></a>  } finally {
<a name="line264"></a>    goog.dom.appendChild(document.body, test3);
<a name="line265"></a>  }
<a name="line266"></a>}
<a name="line267"></a>
<a name="line268"></a>function testGetEndPositionReversed() {
<a name="line269"></a>  expectedFailures.expectFailureFor(goog.userAgent.GECKO &amp;&amp;
<a name="line270"></a>      !goog.userAgent.isVersionOrHigher(&#39;2&#39;));
<a name="line271"></a>
<a name="line272"></a>  // Simulate the user selecting backwards from right-to-left.
<a name="line273"></a>  // The end node is now in the top left.
<a name="line274"></a>  var firstNode = test3.firstChild.firstChild;
<a name="line275"></a>  var lastNode = test3.lastChild.lastChild;
<a name="line276"></a>  var range = goog.dom.TextRange.createFromNodes(
<a name="line277"></a>      lastNode, lastNode.nodeValue.length, firstNode, 0);
<a name="line278"></a>  var topLeft = goog.style.getPageOffset(test3.firstChild);
<a name="line279"></a>
<a name="line280"></a>  if (goog.userAgent.IE) {
<a name="line281"></a>    // On IE the selection is as tall as its tallest element.
<a name="line282"></a>    var logoPosition = goog.style.getPageOffset(logo3);
<a name="line283"></a>    topLeft.y = logoPosition.y;
<a name="line284"></a>
<a name="line285"></a>    if (!goog.userAgent.isVersionOrHigher(&#39;8&#39;)) {
<a name="line286"></a>      topLeft.x += 2;
<a name="line287"></a>      topLeft.y += 2;
<a name="line288"></a>    }
<a name="line289"></a>  }
<a name="line290"></a>
<a name="line291"></a>  try {
<a name="line292"></a>    var result = assertNotThrows(goog.bind(range.getEndPosition, range));
<a name="line293"></a>    assertObjectEquals(topLeft, result);
<a name="line294"></a>  } catch (e) {
<a name="line295"></a>    expectedFailures.handleException(e);
<a name="line296"></a>  }
<a name="line297"></a>}
<a name="line298"></a>
<a name="line299"></a>function testGetEndPositionRightToLeft() {
<a name="line300"></a>  expectedFailures.expectFailureFor(goog.userAgent.GECKO &amp;&amp;
<a name="line301"></a>      !goog.userAgent.isVersionOrHigher(&#39;2&#39;));
<a name="line302"></a>  expectedFailures.expectFailureFor(goog.userAgent.IE &amp;&amp;
<a name="line303"></a>      !goog.userAgent.isVersionOrHigher(&#39;8&#39;));
<a name="line304"></a>
<a name="line305"></a>  // Even in RTL content the end node is still in the bottom right.
<a name="line306"></a>  var range = goog.dom.TextRange.createFromNodeContents(test3Rtl);
<a name="line307"></a>  var pageOffset = goog.style.getPageOffset(test3Rtl.lastChild);
<a name="line308"></a>  var bottomRight = new goog.math.Coordinate(
<a name="line309"></a>      pageOffset.x + test3Rtl.lastChild.offsetWidth,
<a name="line310"></a>      pageOffset.y + test3Rtl.lastChild.offsetHeight);
<a name="line311"></a>
<a name="line312"></a>  if (goog.userAgent.IE &amp;&amp; !goog.userAgent.isVersionOrHigher(&#39;8&#39;)) {
<a name="line313"></a>    bottomRight.x += 2;
<a name="line314"></a>    bottomRight.y += 2;
<a name="line315"></a>  }
<a name="line316"></a>
<a name="line317"></a>  try {
<a name="line318"></a>    var result = assertNotThrows(goog.bind(range.getEndPosition, range));
<a name="line319"></a>    assertObjectRoughlyEquals(bottomRight, result, 1);
<a name="line320"></a>  } catch (e) {
<a name="line321"></a>    expectedFailures.handleException(e);
<a name="line322"></a>  }
<a name="line323"></a>}
</pre>


</body>
</html>
